﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.Core.Repository;
using TaobaoERP.Entity;
using TaobaoERP.BusinessTaobao.TaobaoAPI;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace TaobaoERP.BusinessServices
{
    public class SellerCatService : RepositoryBase
    {
        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="item"></param>
        public static void Add(SellerCat item)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                db.GetTable<SellerCat>().InsertOnSubmit(item);
                db.SubmitChanges();
            }
        }

        public SellerCat GetInfo(int id)
        {
             using (RepositoryContext db = new RepositoryContext())
            {
                SellerCat item = GetEntity<SellerCat>(u => u.ID == id);
                return item;
             }
        }
        public bool IsExistLabel(int? id,string Name)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                int msg = 0;
                if (id.HasValue)
                {
                    var item = from tb in db.GetTable<SellerCat>()
                           where tb.Name == Name && tb.ID != id.Value
                           select tb;
                    msg=item.Count();
                }
                else
                {
                    var item = from tb in db.GetTable<SellerCat>()
                           where tb.Name == Name 
                           select tb;
                    msg=item.Count();
                }
                //SellerCat item = GetEntity<SellerCat>(u => u.Name == Name);
                
                
                if (msg > 0)
                    return false;
                else
                    return true;
                //return item == null ? false : true;
                //return db.GetTable<SellerCat>().Single(p => p.Name == Name) == null ? false : true;
            }
        }
        public void Del(int id)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var f = from fd in db.GetTable<SellerCat>()
                        where (fd.ID == id)
                        select fd;
                db.GetTable<SellerCat>().DeleteAllOnSubmit(f);
                db.SubmitChanges();
                //return db.GetTable<SellerCat>().Single(p => p.Name == Name) == null ? false : true;
            }
        }
        public void update(SellerCat item)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var _sellerCat = db.GetTable<SellerCat>().First<SellerCat>(u => u.ID == item.ID);
                _sellerCat.Name = item.Name;
                db.SubmitChanges();
            }
        }
        /// <summary>
        /// 商品分类
        /// </summary>
        public static void UpdateSellerCat(string nick)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                //TaobaoAPI orderProcessor = PolicyInjection.Create<TaobaoAPI>(SessionID);
                ItemCatsAPI orderProcessor = new ItemCatsAPI();
                List<SellerCat> listItemcats = orderProcessor.sellercats_list_get(nick);
                foreach (var item in listItemcats)
                {
                    int msg = (from f in db.GetTable<SellerCat>() where f.Cid == item.Cid select f.ID).Count();
                    if (msg == 0)
                    {
                        item.Nick = nick;
                        db.GetTable<SellerCat>().InsertAllOnSubmit<SellerCat>(listItemcats);
                    }
                }

                db.SubmitChanges();
            }
        }
        public static List<SellerCat> ListSellerCat(string Nick,int PageIndex)
        {
            RepositoryContext db = new RepositoryContext();
            var query = from f in db.GetTable<SellerCat>()
                        where f.Nick == Nick 
                        orderby f.Cid descending
                        select new
                        {
                            ID = f.ID,
                            IsParent = f.IsParent,
                            Name = f.Name,
                            ParentID = f.ParentID,
                            Cid=f.Cid
                        }
                        
                        ;
            query = query.Skip(10 * PageIndex).Take(10);
            return db.ExecuteQuery<SellerCat>(query);
        }

        public static int CountSellerCat(string Nick)
        {
            RepositoryContext db = new RepositoryContext();
            return db.GetTable<SellerCat>().Where(p => p.Nick == Nick).Count();
        }
    }
}
